/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit.text;

import java.awt.geom.Point2D;
import java.util.Vector;

import scenic.SceneContainer;
import scenic.SceneNode;
import scenic.Graphics;
import scenic.toolkit.CComponent;

public class CParagraph extends CComponent {
	private ParagraphStyle style;
	private Vector<Element> elements;
	
	private double top;
	private Point2D size;
	private double sizeWidth;
		
	public CParagraph() {
		elements = new Vector<Element>();
	}
	
	Vector<Element> getElements() {
		return elements;
	}
	
	public void add(Element element) {
		if(element.getParagraph() != null)
			element.getParagraph().remove(element);
		element.setParagraph(this);
		elements.add(element);
		addChild(element.getComponent());
	}

	public void remove(Element element) {
		element.setParagraph(null);
		elements.remove(element);
		removeChild(element.getComponent());
	}
	
	public ParagraphStyle getStyle() {
		return style;
	}
	
	public void setStyle(ParagraphStyle style) {
		this.style = style;
	}
	
	public double getTop() {
		return top;
	}
	
	public void setTop(double top) {
		this.top = top;
	}
	
	public void paint(Graphics g) {
		ParagraphIterator itr = new ParagraphIterator(this, getWidth());
		Vector<ElementItem> objects = new Vector<ElementItem>();
		ElementItem object;
		SceneContainer scene = getScene();
		
		scene.clear();
		
		while(itr.hasNext()) {
			object = itr.next();
			
			if(objects.size() > 0 && object.element != objects.elementAt(0).element) {
				objects.elementAt(0).element.drawObjects(scene, objects);
				objects.clear();
			}
			objects.add(object);
		}
		if(objects.size() > 0) {
			objects.elementAt(0).element.drawObjects(scene, objects);
		}
	}
	
	public Point2D getMinimumSizeFor(double width, double height) {
		if(size != null && sizeWidth == width)
			return size;
		
		ParagraphIterator itr = new ParagraphIterator(this, width);
		double w = 0.0;

		sizeWidth = width;
		
		while(itr.hasNext()) {
			ElementItem item = itr.next();
			
			if(!item.isSpace) {
				w = Math.max(w, item.x + item.width);
			}
		}
		size = new Point2D.Double(w, itr.getLineTop());
		
		return size;
	}
}
